#!/bin/bash

source ../common/common.sh

echo "===INIT==UBUNTU=GIT==="
sudo echo "SUDO ROOT"

readinput_git_user_pass()
{
  local git_host=$1
  if [ ! -z "$git_host" ]; then
    read -p "GIT站点 $git_host 配置git用户名: " git_name
    read -s -p "GIT站点 $git_host 配置git默认密码: " git_pass
    if [ ! -z "$git_name" -a ! -z "$git_pass" ]; then
      echo "machine $git_host login $git_name password $git_pass"
    fi
  fi
}

show t "安装 git"
install_apt_pack "force|git"

if [ `is_desktop` -eq 1 ]; then
  show t "安装 gitg qgit"
  install_apt_pack "force|gitg qgit"
fi
read -p "配置Git参数(y/n/)?[y=输入配置 n=不配置] " RESP


if [ "$RESP" == "y" ]; then
  show t "配置git全局用户信息: "
  read -p "GIT用户: " GIT_NAME
  read -p "GIT邮件: " GIT_EMAIL
  if [ ! -z "$GIT_NAME" -a ! -z "$GIT_EMAIL" ]; then 
    show i "config git --global $GIT_NAME info"
    git config --global user.name "$GIT_NAME"
    git config --global user.email "$GIT_EMAIL"
    git config --global push.default simple
  else
    show i "no config"
  fi
else
  show i "未配置git信息将启动 simple 模式"
fi

git config --global color.ui true
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.rt "reset --hard"
git config --global alias.ls ls-files
git config --global alias.unstage "reset HEAD"
git config --global alias.uncommit "reset --soft HEAD^"
git config --global alias.l "log --pretty=oneline --abbrev-commit --graph --decorate"
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.l2d "log --since='2 days'"
git config --global alias.who "shortlog -n -s --no-merges"
git config --global alias.g "grep -n --color -E"
git config --global alias.cp "cherry-pick -x" 
git config --global alias.cb "checkout -b"
git config --global branch.autosetuprebase always
git config --global color.status auto
git config --global color.branch auto
git config --global core.editor vim
git config --global merge.tool vimdiff
git config --global alias.last 'log -1'

RESP=""
read -p "配置Git默认密码(y/n)?[y=输入配置 n=不配置] " RESP
if [ "$RESP" == "y" ]; then
  if [ -f ~/.netrc ]; then 
    rm ~/.netrc
  fi
  line=`readinput_git_user_pass git.oschina.net`
  if [ ! -z "$line" ]; then 
    echo "$line" >> ~/.netrc
  fi
  echo ""
  line=`readinput_git_user_pass github.com`
  if [ ! -z "$line" ]; then 
    echo "$line" >> ~/.netrc
  fi
  echo ""
  line=`readinput_git_user_pass bitbucket.org`
  if [ ! -z "$line" ]; then 
    echo "$line" >> ~/.netrc
  fi
  echo ""
fi

show i "git configs"
echo "  http://blog.javachen.com/2013/12/27/some-git-configs-and-cammands/"

